package com.pt.common.utils.uuid;

import java.security.SecureRandom;
import java.util.Set;

/**
 * @author like
 * @date 2025/2/21 22:40
 * @Description TODO
 */
public class code {
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int CODE_LENGTH = 8;
    private static final SecureRandom random = new SecureRandom();

    /**
     * 生成一个随机的课程码
     *
     * @return 随机课程码
     */
    public static String generateRandomCourseCode() {
        StringBuilder sb = new StringBuilder(CODE_LENGTH);
        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }
        return sb.toString();
    }

    public static String generateUniqueCode(Set<String> codeSet) {
        String newCode = code.generateRandomCourseCode();
        // 如果给定的 code 不在 codeSet 中，则直接返回给定的 code
        while (codeSet.contains(newCode)) {
            newCode = code.generateRandomCourseCode();
        }

        return newCode;
    }
}
